<template>
  <div class="question-list-container">
    <!--查询表单-->

    <el-form :inline="true" class="demo-form-inline">

      <el-form-item prop="date" label="添加时间：">
        <el-date-picker
          v-model="date"
          type="daterange"
          align="right"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至 "
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>

      <el-form-item >
        <el-input v-model="searchParams.title" placeholder="问答标题" maxlength="30" clearable show-word-limit/>
      </el-form-item>

<!--      <el-form-item>-->
<!--        <el-select-->
<!--          v-model="searchParams.tag"-->
<!--          filterable-->
<!--          placeholder="请选择问答标签">-->
<!--          <el-option-->
<!--            v-for="tag in tagList"-->
<!--            :key="tag.id"-->
<!--            :label="tag.name"-->
<!--            :value="tag.id"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-button type="primary" icon="el-icon-search" @click="fetchData()" size="medium">查询</el-button>
      <el-button type="warning" icon="el-icon-refresh" @click="resetData()" size="medium">清空</el-button>
    </el-form>

    <!--表格-->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="title" label="问答标题" align="center" />

      <el-table-column prop="tag" label="问答标签" width="210" align="center"/>

      <el-table-column prop="reply" label="回复数" align="center" width="80"/>

      <el-table-column prop="click" label="访问数" align="center" width="80"/>

      <el-table-column prop="gmtCreate" label="添加时间" width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.gmtCreate.slice(0,10)}}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-view" @click="handleEdit(scope.row.questionId)">修改</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleRemove(scope.row.questionId)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
  import { getAllTag } from '@/api/tag'
  import { getQuestionPageList, deleteQuestionById } from '@/api/question'

  export default {
    data(){
      return{
        listLoading: true, // 是否显示loading信息
        list: null, // 数据列表
        total: 0, // 总记录数
        page: 1, // 页码
        limit: 10, // 每页记录数
        searchParams: {},// 查询条件
        date: '', //日期
        pickerOptions: {
          //不可选择当天以后日期
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6;
          },

          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          },]
        },
        tagList: []
      }
    },

    methods: {
      fetchData(page = 1) {
        if (this.date){
          this.searchParams.begin = this.date[0]
          this.searchParams.end = this.date[1]
        }
        // 当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
        this.page = page
        this.listLoading = true

        getQuestionPageList(this.page, this.limit, this.searchParams)
          .then(result => {
            if (result.success == true) {
              this.list = result.data.rows
              this.total = result.data.total

              //将标签id转换为名称
              for (let i = 0; i < this.list.length; i++){
                this.list[i].tag = this.list[i].tag.split(',')
                this.list[i].tag.forEach(item => {
                  this.tagList.forEach(temp => {
                    if (temp.id === item) {
                      this.list[i].tag.push(temp.name + '、')
                    }
                  })
                })
                if (this.list[i].tag.length == 2) {
                  this.list[i].tag.splice(0,1)
                  this.list[i].tag[0] = this.list[i].tag[0].substring(0, this.list[i].tag[0].length-1)
                }
                if (this.list[i].tag.length == 4) {
                  this.list[i].tag.splice(0,2)
                  this.list[i].tag[1] = this.list[i].tag[1].substring(0, this.list[i].tag[1].length-1)
                }
                if (this.list[i].tag.length == 6) {
                  this.list[i].tag.splice(0, 3)
                  this.list[i].tag[2] = this.list[i].tag[2].substring(0, this.list[i].tag[2].length-1)
                }
              }
            }
            this.listLoading = false
          })
      },

      resetData() {
        this.date = ''
        this.searchParams = {}
        this.fetchData()
      },

      initTagList(){
        getAllTag().then(result => {
          if (result.code === 20000){
            this.tagList = result.data.items
          }
        })
      },

      handleEdit(id){
        this.$router.push("/manage/question/edit/"+id)
      },

      handleRemove(id){
        this.$confirm('此操作将永久删除该问答与该问答下所有回复, 本操作不可逆，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return deleteQuestionById(id)
        }).then(result => { // 如果上一个then成功则执行此处的then回调
          if (result.code === 20000){
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
          }
        }).catch((response) => {
          if (response === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除！'
            })
          }
        })
      }

    },

    created() {
      this.initTagList()
      this.fetchData()
    }
  }
</script>

<style scoped>
  .question-list-container{
    margin-top: 20px;
    margin-left: 15px;
  }
</style>
